package com.uenpay.im.ui.old.activity.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.util.app.base.BaseActivity;

/**
 * @author SL
 */
public class MyRedPagerActivity extends BaseActivity {

    private TextView my_money;
    private View loading;
    private String balance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_my_cash_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
        getData();
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview)).setText(getResources()
                .getString(R.string.red_wallet));
        ((TextView) findViewById(R.id.title_right_tv)).setText(getResources()
                .getString(R.string.red_balance_payments));
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
        my_money = (TextView) findViewById(R.id.my_money);
        loading = findViewById(R.id.loading);
    }

    protected void initListener() {
        findViewById(R.id.title_right_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Intent intent = new Intent(MyRedPagerActivity.this,
                                StatementActivity.class);
                        startActivityForResult(intent, 100);
                    }
                });
        findViewById(R.id.recharge_btn).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Intent intent = new Intent(MyRedPagerActivity.this,
                                ChargeActivity.class);
                        startActivityForResult(intent, 100);
                    }
                });
        findViewById(R.id.withdrawals_btn).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Intent intent = new Intent(MyRedPagerActivity.this,
                                CashActivity.class);
                        intent.putExtra("balance", balance);
                        startActivityForResult(intent, 100);
                    }
                });
    }

    private void getData() {
        loading.setVisibility(View.VISIBLE);
        WalletHandle.getBalance(new WalletHandle.IGetBalanceResultListener() {

            @Override
            public void onGetBalanceResult(String balance, int status,
                                           int haspaypassword) {
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    MyRedPagerActivity.this.balance = balance;
                    if (TextUtils.isEmpty(balance)) {
                        my_money.setText("¥0.00");
                    } else {
                        my_money.setText("¥" + balance);
                    }
                }
                myHandler.sendEmptyMessage(0);
            }
        });

    }

    private Handler myHandler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0) {
                if (loading != null) {
                    loading.setVisibility(View.GONE);
                }
            }

            return false;
        }
    });

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if ((requestCode == 100 || requestCode == 101)
                && resultCode == RESULT_OK) {
            getData();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
